package com.kch.web.controller.system;

import com.kch.common.annotation.Log;
import com.kch.common.core.controller.BaseController;
import com.kch.common.core.domain.AjaxResult;
import com.kch.common.core.page.TableDataInfo;
import com.kch.common.enums.BusinessType;
import com.kch.framework.util.ShiroUtils;
import com.kch.system.domain.Activity;
import com.kch.system.service.IActivityService;
import com.kch.system.service.ISysTagService;
import com.kch.web.config.CloumnProperties;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
@RequestMapping("/system/activity")
@Api(tags = "活动管理")
public class ActivityController extends BaseController {
    private String prefix = "system/activity";

    @Autowired
    private IActivityService activityService;
    @Autowired
    private ISysTagService sysTagService;
    @Autowired
    private CloumnProperties cloumnProperties;

    @RequiresPermissions("system:activity:view")
    @GetMapping()
    public String index(ModelMap mmap) {
        List<String> collect = Stream.of(cloumnProperties.getActivity()).collect(Collectors.toList());
        mmap.put("tags", sysTagService.selectByCloumnCode(collect));
        return prefix + "/activity";
    }

    @PostMapping("/list")
    @RequiresPermissions("system:activity:list")
    @ResponseBody
    public TableDataInfo list(Activity activity) {
        startPage();
        List<Activity> list = activityService.selectMeetingPage(activity);
        return getDataTable(list);
    }

    @GetMapping("/status")
    @RequiresPermissions("system:meeting:list")
    @ResponseBody
    public AjaxResult status(Integer id) {
        try {
            activityService.updateStatus(id);
            return toAjax(true);
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<String> collect = Stream.of(cloumnProperties.getActivity()).collect(Collectors.toList());
        mmap.put("tags", sysTagService.selectByCloumnCode(collect));
        return prefix + "/add";
    }

    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Validated Activity activity) {
        activity.setCreateBy(ShiroUtils.getLoginName());
        activity.setCloumnCode(cloumnProperties.getActivity());
        activityService.insertSysActivity(activity);
        return toAjax(true);
    }

    @Log(title = "活动类型", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(activityService.deleteActivity(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        Activity activity = activityService.selectById(id);
        List<String> collect = Stream.of(cloumnProperties.getActivity()).collect(Collectors.toList());
        mmap.put("tags", sysTagService.selectByCloumnCode(collect));
        mmap.put("activity", activity);
        return prefix + "/edit";
    }

    @Log(title = "会议", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Activity activity) {
        try {
            activity.setUpdateBy(ShiroUtils.getLoginName());
            activity.setCloumnCode(cloumnProperties.getActivity());
            return toAjax(activityService.updateById(activity));
        }catch (Exception e){
            e.printStackTrace();
            return toAjax(false);
        }
    }

    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap mmap) {
        Activity activity = activityService.selectById(id);
        mmap.put("activity", activity);
        return prefix + "/detail";
    }
}
